package com.ysyc.driver_ee.fragment;

import android.app.Dialog;
import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.ysyc.driver_ee.R;
import com.ysyc.driver_ee.adapter.OrderCompletedAdapter;
import com.ysyc.driver_ee.api.HttpResource;
import com.ysyc.driver_ee.api.NetworkCallback;
import com.ysyc.driver_ee.api.RetrofitClient;
import com.ysyc.driver_ee.base.BaseFragment;
import com.ysyc.driver_ee.factory.RecyclerViewFactory;
import com.ysyc.driver_ee.model.OrderForm;

import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * <p>描述：(这里用一句话描述这个类的作用)</p>
 * 作者： Chan<br>
 * 日期： 2018/11/3 14:49<br>
 * 版本： v1.0<br>
 */
public class OrderCompletedFragment extends BaseFragment {
    @BindView(R.id.xrv_order_completed_view)
    XRecyclerView xrvOrderCompletedView;
    public int currentPage = 0;
    public int REFRESH_TYPE = 0x001;
    public int LOADMORE_TYPE = 0x002;
    public int rvType = 0x000;

    private NetworkCallback<List<OrderForm>> listNetworkCallback;
    private int type;
    private OrderCompletedAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_order_completed;
    }

    @Override
    protected void onFragmentCreate(View view) {

        RecyclerViewFactory.initVerticalXRvDecoration(getContext(), xrvOrderCompletedView, 10);
        adapter = new OrderCompletedAdapter(getActivity(), R.layout.rv_order_form_already_sent_item);
        xrvOrderCompletedView.setAdapter(adapter);
        initView();
    }


    private void initView() {
        xrvOrderCompletedView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                currentPage = 0;
                rvType = REFRESH_TYPE;
                getOrderFormList();
            }

            @Override
            public void onLoadMore() {
                currentPage++;
                rvType = LOADMORE_TYPE;
                getOrderFormList();
            }
        });
        initNetworkRequest();

    }

    private void initNetworkRequest() {
        listNetworkCallback = new NetworkCallback<>(new HttpResource<List<OrderForm>>() {
            @Override
            public void onSuccess(List<OrderForm> orderFormList) {
                if (rvType == REFRESH_TYPE) {
                    xrvOrderCompletedView.refreshComplete();
                    if (orderFormList == null || orderFormList.size() == 0) {
//                        if (view == null) {
//                            LogUtils.e("添加");
//                            view = LayoutInflater.from(getContext()).inflate(R.layout.xrv_head_order_form, xrvOrderFormView, false);
//                            xrvOrderFormView.addHeaderView(view);
//                        }
                        xrvOrderCompletedView.setNoMore(true);
                    } else if (orderFormList.size() > 0 && orderFormList.size() < 20) {
//                        if (null != view) {
//                            xrvOrderFormView.removeView(view);
//                        }
                        adapter.bindingList(orderFormList);
                        xrvOrderCompletedView.setNoMore(true);
                    } else if (orderFormList.size() == 20) {
//                        if (null != view) {
//                            xrvOrderFormView.removeView(view);
//                        }
                        adapter.bindingList(orderFormList);
                        xrvOrderCompletedView.setNoMore(false);
                    }
                } else if (rvType == LOADMORE_TYPE) {
                    xrvOrderCompletedView.loadMoreComplete();
//                    if (null != view) {
//                        xrvOrderFormView.removeView(view);
//                    }
                    if (orderFormList == null || orderFormList.size() == 0) {
                        xrvOrderCompletedView.setNoMore(true);
                    } else if (orderFormList.size() > 0 && orderFormList.size() < 20) {
                        adapter.addDataList(orderFormList);
                        xrvOrderCompletedView.setNoMore(true);
                    } else if (orderFormList.size() == 20) {
                        adapter.addDataList(orderFormList);
                        xrvOrderCompletedView.setNoMore(false);
                    }
                }
            }

            @Override
            public void onFailure(String errorDes) {
                if (rvType == REFRESH_TYPE) {
                    xrvOrderCompletedView.refreshComplete();
                } else if (rvType == LOADMORE_TYPE) {
                    xrvOrderCompletedView.loadMoreComplete();
                }
            }

            @Override
            public void onLoading(Dialog dialog) {

            }
        });
    }


    public void getOrderFormList() {
        Observable<List<OrderForm>> orderFormItemDriverListObservable = RetrofitClient.getInstance().getHttpServices().orderformListSend(20, currentPage);
        listNetworkCallback.postRequest(orderFormItemDriverListObservable, null);
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        if (isVisible) {
            xrvOrderCompletedView.refresh();
        }
    }


}
